package com.hotelsystem.reservations.hotels;

import java.util.List;

import com.hotelsystem.reservations.interfaces.HotelInterface;

public class HotelManagement implements HotelInterface {

	private List<Hotel> hotels;

	public void setHotels(List<Hotel> hotels) {
		this.hotels = hotels;
	}

	public boolean updateOrAddHotelDetails(Hotel hotel) {
		Hotel hotelToModify = hasHotelInList(hotel);

		if (hotels != null && hotels.size() > 0 && hotelToModify != null) {
			// Update hotel
			updateHotelInformation(hotelToModify, hotel);
		} else {
			// Add hotel
			hotels.add(hotel);
		}

		return true;
	}

	public List<Hotel> getHotels() {
		return hotels;
	}

	@Override
	public String toString() {
		return "Admin [hotels=" + hotels + "]";
	}

	private Hotel hasHotelInList(Hotel hotel) {
		String searchedHotelName = hotel.getHotelInformation().getName();
		String searchedHotelAddress = hotel.getHotelInformation().getAddress();

		for (Hotel singleHotel : hotels) {
			if (singleHotel.getHotelInformation().getName()
					.equals(searchedHotelName)
					&& singleHotel.getHotelInformation().getAddress()
							.equals(searchedHotelAddress)) {
				return singleHotel;
			}
		}

		return null;
	}

	private void updateHotelInformation(Hotel hotelToModify, Hotel newHotel) {
		HotelInformation updatedInformation = hotelToModify
				.getHotelInformation();
		HotelInformation newInformation = newHotel.getHotelInformation();

		hotelToModify.setHotelInformation(newHotel.getHotelInformation());
	}

}
